package controller;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileUploadException;

public abstract class MyCoolServlet extends HttpServlet {

    protected String index = null;
    protected String carrito = null;
    protected String errorForm = null;
    protected String displayForm = null;
    protected String displayUserForm = null;
    protected String successVentaForm = null;
    protected String successForm = null;
    protected String confirmCarritoForm = null;
    protected String updateForm2 = null;
    protected String createServlet = null;
    protected String createForm = null;
    protected String createUserServlet = null;
    protected String deleteServlet = null;
    protected String deleteUserServlet = null;
    protected String updateServlet = null;
    protected String updateUserServlet = null;
    protected String readServlet = null;
    protected String readUserServlet = null;
    protected String signInServlet = null;
    protected String signOutServlet = null;
    protected String addToCarritoServlet = null;
    protected String deleteCarritoServlet = null;
    protected String confirmCarritoServlet = null;
    protected String procesarImagenServlet = null;
    protected String mailServlet = null;
    protected String persistenceMechanism = null;

    protected abstract void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, FileUploadException;

    protected void gotoNamedResource(String address, HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        RequestDispatcher dispatcher = getServletContext().getNamedDispatcher(address);
        dispatcher.forward(request, response);
    }

    protected void gotoURL(String address, HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(address);
        dispatcher.forward(request, response);
    }

    @Override
    public void init() {
        ServletConfig config = getServletConfig();
        ServletContext context = config.getServletContext();
        index = context.getInitParameter("index");
        carrito = context.getInitParameter("carrito");
        displayForm = context.getInitParameter("displayForm");
        displayUserForm = context.getInitParameter("displayUserForm");
        successVentaForm = context.getInitParameter("successVentaForm");
        confirmCarritoForm = context.getInitParameter("confirmCarritoForm");
        errorForm = context.getInitParameter("errorForm");
        successForm = context.getInitParameter("successForm");
        updateForm2 = context.getInitParameter("updateForm2");
        createServlet = context.getInitParameter("createServlet");
        createForm = context.getInitParameter("createForm");
        createUserServlet = context.getInitParameter("createUserServlet");
        deleteServlet = context.getInitParameter("deleteServlet");
        deleteUserServlet = context.getInitParameter("deleteUserServlet");
        readServlet = context.getInitParameter("readServlet");
        readUserServlet = context.getInitParameter("readUserServlet");
        updateServlet = context.getInitParameter("updateServlet");
        updateUserServlet = context.getInitParameter("updateUserServlet");
        signInServlet = context.getInitParameter("signInServlet");
        signOutServlet = context.getInitParameter("signOutServlet");
        addToCarritoServlet = context.getInitParameter("addToCarritoServlet");
        deleteCarritoServlet = context.getInitParameter("deleteCarritoServlet");
        confirmCarritoServlet = context.getInitParameter("confirmCarritoServlet");
        procesarImagenServlet = context.getInitParameter("procesarImagenServlet");
        mailServlet = context.getInitParameter("mailServlet");
        persistenceMechanism = context.getInitParameter("persistenceMechanism");
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (FileUploadException ex) {
            Logger.getLogger(MyCoolServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (FileUploadException ex) {
            Logger.getLogger(MyCoolServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
